PROJECT_NAME="CSSonarDemo.xcworkspace"
TARGET_NAME="CSSonarDemo"

TEST_TARGET_NAME="CSSonarDemoTests"
UITEST_TARGET_NAME="CSSonarDemoUITests"

rm -rf sonar-reports

mkdir sonar-reports

rm -rf ./build/derivedData

xcodebuild clean -UseModernBuildSystem=NO

xcodebuild -workspace ${PROJECT_NAME} -scheme ${TARGET_NAME} -UseModernBuildSystem=NO -derivedDataPath ./build/derivedData -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO | xcpretty -r json-compilation-database -o compile_commands.json

# 生成OCLint分析报告
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html
oclint-json-compilation-database -e Pods -- -report-type pmd -o sonar-reports/oclint.xml \
-max-priority-1=9999 \
-max-priority-2=9999 \
-max-priority-3=9999 \

# 运行单元测试
xcodebuild -workspace ${PROJECT_NAME} -scheme ${TEST_TARGET_NAME} -configuration Debug -enableCodeCoverage YES -destination 'platform=iOS Simulator,name=iPhone 8' test
# 生成单测覆盖率报告
slather coverage -x --output-directory sonar-reports --scheme ${TARGET_NAME} --workspace ${PROJECT_NAME} CSSonarDemo.xcodeproj

# 重命名覆盖率报告（sonar-swift插件默认指定路径为`sonar-reports/coverage*.xml`）
mv sonar-reports/cobertura.xml sonar-reports/coverage.xml

# Sonar扫描
/bin/sh sonar-scanner -X
